package com.kgdev.cweb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.kgdev.cweb.beans.pub.impl.PlateArticleBean;
import com.kgdev.cweb.entities.pub.PlateArticle;

@FacesConverter(forClass = PlateArticle.class)
public class PlateArticleConverter implements Converter {

	private static PlateArticleBean plateArticleBean;

	{
		try {
			InitialContext ic = new InitialContext();
			plateArticleBean = (PlateArticleBean) ic
					.lookup("java:global/ChocolorEAR/ChocolorEJB/PlateArticleBean!com.kgdev.cweb.beans.pub.impl.PlateArticleBean");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		String plateArticleId = value;
		PlateArticle plateArticle = plateArticleBean.find(plateArticleId);
		return plateArticle;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		PlateArticle plateArticle = (PlateArticle) value;
		return plateArticle.getArticleId();
	}

}
